home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag05 / sorting.swg < prev    next >
Encoding:
Text File  |  1994-09-22  |  9.0 KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00003                                                                           1      05-25-9408:19ALL                      TED HANSEN               Re: Sorting an Array of  SWAG9405            22     S   (*π -=> Quoting Tim Benoit to All <=-ππ TB> I am having a bit of difficulty figuring out how to sort anπ TB> array of records by numerical or alphabetical order.π TB>                  I need all the information that goes with thatπ TB> specific Record to stay with it...ππ    This example uses a modified bubblesort algorithm, not real fast butπfairly easy to follow. You may want to use a faster sort procedure butπthe basic idea is to examine the data in your selected sortπfield (RecArray[i].variable) but do the sort on the wholeπrecord (RecArray[i]):ππeg:π     Varππ         Buffer : Rec;ππ     If RecArray[3].Number1 > RecArray[4].Number1 then { sort }ππ        Begin   { interchange RecArray[3] and RecArray[4] }π                Buffer := RecArray[3];π                RecArray[3] := RecArray[4];π                RecArray[4] := Bufferπ        End;ππBubblesort makes multiple passes moving data only one place per pass.πThis example is similar but uses only one pass.π*)ππProgram Modsort;ππuses Crt;      {only needed for clrscr}ππConstπ  max = 10;      {max number of records}ππTypeπ  fieldtype = string[2];π  datatype = recordπ         rec1 : fieldtype;π         rec2 : fieldtype;π         end;πVarπ  data : array [1..max] of datatype;π  i,j : byte;ππProcedure interchange(r,l:datatype);ππVarπ   buffer : datatype;ππBeginπ     buffer := r;π     data[i] := l;π     data[i+1] := buffer;π     dec(i);πEnd;ππProcedure sort(j : byte);  {j is the selected sort field number}ππVarπ   field : array [1..2] of fieldtype;ππBeginπ   i := 1;ππ   While i < max  doπ         Beginπ            Case j ofπ                1 : Beginπ                       field[1] := data[i].rec1;π                       field[2] := data[i+1].rec1;π                    End;π                2 : Beginπ                       field[1] := data[i].rec2;π                       field[2] := data[i+1].rec2;π                    End;π            End;ππ         If field[1] > field[2] thenπ            Interchange(data[i], data[i+1])π         Elseπ            Inc(i);π         End;πEnd;ππBegin                              {main}ππClrscr;πWriteln('UNSORTED :');πFor i := 1 to max do              {make up random array of alphas}π    Beginπ        j := random(26);π        data[i].rec1 := chr(j+65);π        Write(data[i].rec1);π        j := random(26);π        Data[i].rec2 := chr(j+65);π        Writeln(',',data[i].rec2);π    End;ππWrite('Sort on which field? ');πReadln(j);πSort(j);πWriteln('SORTED ON FIELD: ',j);ππFor i := 1 to max doπ    Beginπ        Write(data[i].rec1);π        Writeln(',',data[i].rec2);π    End;ππEnd.π                                                                                                 2      05-25-9408:20ALL                      MIKE COPELAND            Sorting                  SWAG9405            11     S   {π DR> Does anyone have a good routine to sort a string array intoπ DR> alphabetical order - I really only know how to do a bubbleπ DR> sort, and that's a bit slow for >1000 in the array...π DR> Preferably written in standard Pascal, as I would like toπ DR> understand it,ππ   Here's the conventional QuickSort (which is also included in the fullπTP/BP packages as examples):π}ππvar T     : string;                                  { swap variable }π    GUESS : array[1..1000] of ^string;    { pointer array of strings }πprocedure L_HSORT (LEFT,RIGHT : word);             { Lo-Hi QuickSort }πvar LOWER,UPPER,MIDDLE : word;π    PIVOT              : string;πbeginπ  LOWER := LEFT; UPPER := RIGHT; MIDDLE := (LEFT+RIGHT) div 2;π  PIVOT := GUESS[MIDDLE]^;π  repeatπ    while GUESS[LOWER]^ < PIVOT do Inc(LOWER);π    while PIVOT < GUESS[UPPER]^ do Dec(UPPER);π    if LOWER <= UPPER thenπ      beginπ        T := GUESS[LOWER]^; GUESS[LOWER]^ := GUESS[UPPER]^;π        GUESS[UPPER]^ := T; Inc (LOWER); Dec (UPPER);π      end;π  until LOWER > UPPER;π  if LEFT < UPPER then L_HSORT (LEFT, UPPER);π  if LOWER < RIGHT then L_HSORT (LOWER, RIGHT)πend;                                                       { L_HSORT }π                                                                          3      05-25-9408:23ALL                      LARRY HADLEY             Sort Object              SWAG9405            38     S   {ππPeterborough, Ontario, CANADAππHi !ππ  If any of you boys have been reading BYTE magazine, you may haveπ  noticed an article in the Dec/93 issue on Directory objects (inπ  C++ however). I was keenly interested in this article, because itπ  showed a quick and easy way to handle directory recursion - whichπ  was necessary for a project I was doing.ππ  While the complete code listings weren't given, they were in C soπ  I couldn't use them directly anyways, so I just wrote my ownπ  object in TP. (Works great, btw)ππ  I've decided to wake this conference up a bit so I'm going to postπ  this stuff over the next couple of days. The first installment isπ  the SORT unit which implements a binary-tree sorting object forπ  the sort method of the directory object. This object is completelyπ  re-usable and extendable (designed so from the ground up) andπ  helps demonstrate more uses for OOP.π----------------------------------------------------------------------π}πUnit SORT;ππINTERFACEππTYPEπ   comparefunc = function(d1, d2 :pointer):integer;π                             { function returns sort value for data  }π   ptree    = ^treenode;π   treenode = recordπ      data  :pointer;π      left,π      right :ptree;π   end;π                              { ****** Abstract sort object ******π                                         Must be inheritedπ                              }π   pSortTree = ^oSortTree;π   oSortTree = OBJECTπ      root    :ptree;π      comp    :comparefunc;ππ      constructor Init(cf :comparefunc);π      destructor  Done;ππ      procedure   InsertNode(n :pointer);π      procedure   DeleteNode(var Node); virtual; { abstract }π      function    ReadLeftNode:pointer;π   end;ππIMPLEMENTATIONππconstructor  oSortTree.Init(cf :comparefunc);πbeginπ   FillChar(self, SizeOf(self), #0); { zero out object data }π   comp := cf; { set "compare" function to user defined far-local }πend;ππdestructor   oSortTree.Done;ππ   procedure disposetree(var t :ptree);π   beginπ      if t=NIL thenπ         EXIT;π      disposetree(t^.left);π      disposetree(t^.right);π      DeleteNode(t^.data);π      dispose(t);π   end;ππbeginπ   disposetree(root);πend;ππprocedure    oSortTree.InsertNode(n :pointer);π   { Insert the data pointer in sorted order, as defined by theπ     passed "compare" functionπ   }π   procedure recursetree(var t :ptree);ππ      procedure PutNode(node :ptree);π      beginπ         node^.right := NIL;π         node^.left  := NIL;π         node^.data  := n;π      end;ππ   beginπ      if comp(n, t^.data)>0 thenπ      beginπ         if t^.right<>NIL thenπ            recursetree(t^.right)π         elseπ         beginπ            New(t^.right);π            PutNode(t^.right);π         end;π      endπ      elseπ      beginπ         if t^.left<>NIL thenπ            recursetree(t^.left)π         elseπ         beginπ            New(t^.left);π            PutNode(t^.left);π         end;π      end;π   end;ππbeginπ   if n<>NIL thenπ      if root=NIL thenπ      beginπ         New(root);π         root^.left  := NIL;π         root^.right := NIL;π         root^.data  := n;π      endπ      elseπ         recursetree(root);πend;ππprocedure    oSortTree.DeleteNode(var Node);π   { The calling code must define how to dispose of the data fieldπ     by inheritance }πbeginπ   Halt(255); {abstract method}πend;ππfunction     oSortTree.ReadLeftNode:pointer;π   { This function is intended to be called one-at-a-time to recoverπ     data in sorted order. The data is returned as an untypedπ     pointer. It is assumed that the calling code will type theπ     pointer as required. The data pointer is set to NIL after beingπ     passed to the caller. }πvarπ   ln :pointer;ππ   procedure recurseTree(var t :pTree;var result :pointer);π   beginπ      if t^.left<>NIL thenπ      beginπ         recurseTree(t^.left, result);π         if result=NIL thenπ         beginπ            result  := t^.data;π            t^.data := NIL;π         end;π      endπ      elseπ      beginπ         if t^.data<>NIL thenπ         beginπ            result  := t^.data;π            t^.data := NIL;π         endπ         elseπ            if t^.right<>NIL thenπ            beginπ               recurseTree(t^.right, result);π               if result=NIL thenπ               beginπ                  dispose(t);π                  t := NIL;π               endπ            endπ            elseπ            beginπ               dispose(t);π               t := NIL;π               result := NIL;π            end;π      end;π   end;ππbeginπ   if root<>NIL thenπ   beginπ      recurseTree(root, ln);π      ReadLeftNode := ln;π   endπ   elseπ      ReadLeftNode := NIL;πend;ππEND.π